package org.iteye.bbjava.util.abstractfactory.concre;

import org.iteye.bbjava.util.abstractfactory.AbstractFactory;
import org.iteye.bbjava.util.abstractfactory.ImplementClassProduct;
import org.iteye.bbjava.util.abstractfactory.InterfaceProduct;
import org.iteye.bbjava.util.interpreter.Context;
import org.iteye.bbjava.util.interpreter.NonTerminalExpression;
import org.iteye.bbjava.util.interpreter.TerminalExpression;

public class JavaEntityFileCodeFactory implements AbstractFactory {

	private String entityXml;
	
	@Override
	public InterfaceProduct createInterface() {
		return null;
	}
	
	public void setEntityXml(String entityXml) {
		this.entityXml = entityXml;
	}

	@Override
	public ImplementClassProduct createImplementClass() {
		Context ctx = new Context();
		ImplementClassProduct icp = null;
		if(null !=this.entityXml && !"".equals(entityXml)){
			ctx.setXml(entityXml);
			NonTerminalExpression nte = new NonTerminalExpression();
			TerminalExpression te = new TerminalExpression();
			
			nte.setContext(ctx);
			te.setContext(ctx);
			String	[]javaEntityName = nte.interpret();
			icp = new ImplementClassProductImpl();
			String propertiesAndMethods = te.createFinalResult("");
			icp.setCodeStr(this.createJavaEntityFileCode(javaEntityName[0], propertiesAndMethods, ""));
			return icp;
		}
		
		return icp;
	}
	
	/**
	 * 产生java实体代码,包含setter,getter
	 * @param className 类名
	 * @param propertiesAndMethods 属性和方法
	 * @param packageName 包名
	 * 
	 * */
	private String createJavaEntityFileCode(String className,String propertiesAndMethods,String packageName){
		StringBuffer sb = new StringBuffer();
		if(null!= className && !"".equals(className) && null!= propertiesAndMethods && !"".equals(propertiesAndMethods)){
			
			if(null != packageName ||!"".equals(packageName)){
				sb.append("package "+packageName+";");
			}
			sb.append("public class "+className+" { ");
			sb.append(propertiesAndMethods);
			sb.append(" } ");
		}		
		return sb.toString();
	}

}
